<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/9/14
 * Time: 16:54
 */

namespace app\www\controller;

use think\Session;
use think\Db;

class Chat extends Common
{

    /*对话列表*/
    public function talkList(){
        header('Content-type:text/html;charset=utf-8');
        $page_size=10;
        $access_token=Session::get("access_token");

        if(request()->isGet()){
            $data=array(
                "module"=>'Chat',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>1,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('list',$result_data['list']);
            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Chat',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>$page,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    //进行对话
    public function message(){
        header("Content-type:text/html;charset=utf-8");
        $getid = request()->param('getid');
        $access_token = Session::get('access_token');
        if(request()->isGet()){
            $data=array(
                "module"=>'Chat',
                "interface"=>'1002',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "getid"=>$getid
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];

            $this->assign('uinfo',$result_data['list']['uinfo']);
            $this->assign('chat_maninfo',$result_data['list']['chat_maninfo']);
            $this->assign('chat_list',$result_data['list']['chat_list']);
            $this->assign('getuserid',$getid);
            return $this->fetch();
        }
    }

    /**
     *提交私信
     **/
    public function saveMessage(){
        $access_token = Session::get('access_token');
        $getid = request()->param('getid');
        $content = request()->param('content');
        if(request()->isAjax()){
            $data=array(
                "module"=>'Chat',
                "interface"=>'1003',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "getid"=>$getid,
                    "content"=>$content
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /*聊天历史记录*/
    public function history()
    {
        header('Content-type:text/html;charset=utf-8');
        $page_size=10;
        $access_token=Session::get("access_token");
        $getid = request()->param('getid');
        if(request()->isGet()){
            $data=array(
                "module"=>'Chat',
                "interface"=>'1005',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>1,
                    "getid"=>$getid,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('chat_list',$result_data['list']);
            $this->assign('getuserid',$getid);
            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Chat',
                "interface"=>'1005',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>$page,
                    "getid"=>$getid,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }



    /*ajax 获取对方发的信息*/
    function loadMessage(){
        $access_token = Session::get('access_token');
        $getid = request()->param('getid');
        $lastid = request()->param('lastid');
        if(request()->isAjax()){
            $data=array(
                "module"=>'Chat',
                "interface"=>'1004',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "getid"=>$getid,
                    "lastid"=>$lastid
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }




}